{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6ae5125",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11ee270b",
   "metadata": {},
   "source": [
    "# face++ 人脸检测/face detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9e715308",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.输入我们需要的API网站注册的API_Key\n",
    "API_Key = \"B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI\"\n",
    "API_secret = \"1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a16ea08e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.准备API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ddb76d70",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.调用方法 POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de1a2717",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.准备url传递参数\n",
    "payload ={\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202108%2F20%2F20210820101310_7952f.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649903284&t=dc2b191f83e7db1a4309e43ac878f883\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "314f493f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.发起请求\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ce73a1d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f8d0ccf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647315291,bfe34ae3-e07c-454e-9e37-afbc96dea663',\n",
       " 'time_used': 251,\n",
       " 'faces': [{'face_token': 'fd914700794abd4430a3eff1b1a88405',\n",
       "   'face_rectangle': {'top': 172, 'left': 363, 'width': 167, 'height': 167},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 0.719, 'threshold': 50.0}}}],\n",
       " 'image_id': 'PrBvxI8BSwtGOJ1b3NVNMg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27e1b080",
   "metadata": {},
   "source": [
    "# 1.人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3b208e2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI'\n",
    "API_sercret = '1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':'e59e1f0a3a1a24b062c227c506653a83',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4e21108b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b7ab530b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 210,\n",
       " 'request_id': '1647315296,a5980eb6-e2bb-4e3d-b89b-a6dccd13e49f',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.0,\n",
       "     'neutral': 99.992,\n",
       "     'disgust': 0.0,\n",
       "     'anger': 0.0,\n",
       "     'surprise': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 0.008},\n",
       "    'beauty': {'female_score': 83.447, 'male_score': 81.467},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 22},\n",
       "    'mouthstatus': {'close': 99.713,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.234,\n",
       "     'other_occlusion': 0.053},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 2.553,\n",
       "     'stain': 15.934,\n",
       "     'acne': 17.151,\n",
       "     'health': 13.64},\n",
       "    'headpose': {'yaw_angle': -12.282271,\n",
       "     'pitch_angle': 10.9819765,\n",
       "     'roll_angle': 2.1223388},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.853},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.853},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.853}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.551},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.009,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.991,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.03,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.97,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 63.627},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.451,\n",
       "      'vector_z_component': 0.955,\n",
       "      'vector_x_component': -0.248,\n",
       "      'vector_y_component': -0.163,\n",
       "      'position_y_coordinate': 0.405},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.529,\n",
       "      'vector_z_component': 0.99,\n",
       "      'vector_x_component': 0.107,\n",
       "      'vector_y_component': -0.094,\n",
       "      'position_y_coordinate': 0.421}}},\n",
       "   'face_rectangle': {'width': 167, 'top': 172, 'left': 363, 'height': 167},\n",
       "   'face_token': 'e59e1f0a3a1a24b062c227c506653a83'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8b2d513",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "62d4c582",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>e59e1f0a3a1a24b062c227c506653a83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>99.992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>83.447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>81.467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>2.553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>15.934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>17.151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>13.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>-12.282271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>10.981977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>2.122339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>0.551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>63.627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.529</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.421</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          e59e1f0a3a1a24b062c227c506653a83\n",
       "attributes.emotion.sadness                                                       0.0\n",
       "attributes.emotion.neutral                                                    99.992\n",
       "attributes.emotion.disgust                                                       0.0\n",
       "attributes.emotion.anger                                                         0.0\n",
       "attributes.emotion.surprise                                                      0.0\n",
       "attributes.emotion.fear                                                          0.0\n",
       "attributes.emotion.happiness                                                   0.008\n",
       "attributes.beauty.female_score                                                83.447\n",
       "attributes.beauty.male_score                                                  81.467\n",
       "attributes.gender.value                                                       Female\n",
       "attributes.age.value                                                              22\n",
       "attributes.mouthstatus.close                                                  99.713\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    0.234\n",
       "attributes.mouthstatus.other_occlusion                                         0.053\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              2.553\n",
       "attributes.skinstatus.stain                                                   15.934\n",
       "attributes.skinstatus.acne                                                    17.151\n",
       "attributes.skinstatus.health                                                   13.64\n",
       "attributes.headpose.yaw_angle                                             -12.282271\n",
       "attributes.headpose.pitch_angle                                            10.981977\n",
       "attributes.headpose.roll_angle                                              2.122339\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.853\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.853\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.853\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         0.551\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.009\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.991\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                              0.03\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                  0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             99.97\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  63.627\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.451\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.955\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.248\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                            -0.163\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.405\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.529\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                              0.99\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                             0.107\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                            -0.094\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.421\n",
       "face_rectangle.width                                                             167\n",
       "face_rectangle.top                                                               172\n",
       "face_rectangle.left                                                              363\n",
       "face_rectangle.height                                                            167"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf6247d0",
   "metadata": {},
   "source": [
    "# 2.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d6a56133",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = \"B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI\"\n",
    "API_secret = \"1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI\"\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "payload ={\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202007%2F08%2F20200708192538_jBxPt.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649907196&t=1b97f83f0bb4618c0d1d187f4a916cb9\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b6ea8b6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "efc25447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "77e97300",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647315302,f48c4bbd-8a73-4adb-8fc7-71501057f6d2',\n",
       " 'time_used': 251,\n",
       " 'faces': [{'face_token': 'a6f0c2aa9c21306b237d25c411535abf',\n",
       "   'face_rectangle': {'top': 381, 'left': 514, 'width': 360, 'height': 360},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 0.039, 'threshold': 50.0}}}],\n",
       " 'image_id': 'Tr9mmWndIyK1CEpcfw3Wrw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3dd8c24a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/compare\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI'\n",
    "API_sercret = '1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens1':'fd914700794abd4430a3eff1b1a88405',\n",
    "    'face_tokens2':'a6f0c2aa9c21306b237d25c411535abf',\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7e5cbe26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [404]>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7779dfac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_message': 'API_NOT_FOUND'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7713cdf3",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'Liu_01.jpeg'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-18620d486691>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 1. 打开本地图片\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mPIL\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mImage\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mImage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Liu_01.jpeg'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\PIL\\Image.py\u001b[0m in \u001b[0;36mopen\u001b[1;34m(fp, mode, formats)\u001b[0m\n\u001b[0;32m   2910\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2911\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfilename\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2912\u001b[1;33m         \u001b[0mfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbuiltins\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"rb\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2913\u001b[0m         \u001b[0mexclusive_fp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2914\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'Liu_01.jpeg'"
     ]
    }
   ],
   "source": [
    "# 1. 打开本地图片\n",
    "from PIL import Image\n",
    "Image.open('Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "914b8d67",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 打开本地图片二进制\n",
    "print(open('Liu_01.jpeg', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21e4f928",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "import requests\n",
    "API_key = 'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov'\n",
    "API_sercret = 'Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4ee2a00",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_sercret,image_path):\n",
    "    \"\"\"该函数为调用face++ face_detect接口\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    files = {'image_file': ('Liu_01.jpeg', open('Liu_01.jpeg', 'rb'), 'multipart/form-data')}\n",
    "    payload = {\n",
    "            'api_key':API_key,\n",
    "            'api_secret':API_sercret\n",
    "            }\n",
    "    \n",
    "\n",
    "    r = requests.post(base_url, params = payload,files = files )\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bb29bbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试\n",
    "face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fb053b5",
   "metadata": {},
   "source": [
    "# 准备人脸元数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c632403a",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0481c180",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_meta_data = face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5cdc62f",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_meta_data_face_token = Liu_meta_data['faces'][0]['face_token']\n",
    "Liu_meta_data_face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d12c1c0",
   "metadata": {},
   "source": [
    "# 给出人脸图片进行相似度查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d59ed895",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 准备测试人脸图片 face2\n",
    "\n",
    "face_detect(API_key,API_sercret,'Liu_02.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f98b397c",
   "metadata": {},
   "outputs": [],
   "source": [
    "face02_token = face_detect(API_key,API_sercret,'Liu_02.jpeg')['faces'][0]['face_token']\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3212fe56",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 人脸对比\n",
    "\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':Liu_meta_data_face_token,\n",
    "    'face_token2':face02_token\n",
    "}\n",
    "r = requests.post(compare_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc7aa4dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5750bb1",
   "metadata": {},
   "source": [
    "# 3.封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ac8552b",
   "metadata": {},
   "source": [
    "## 1.人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d92d41f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "83d80554",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "API_key = 'B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI'\n",
    "API_sercret = '1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI'\n",
    "img_url_01 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202108%2F20%2F20210820101310_7952f.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649903284&t=dc2b191f83e7db1a4309e43ac878f883'\n",
    "img_url_02 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F07%2F20200407174156_2Svds.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650459946&t=f7c8a181b21e9d4e72cebb5f10c25816'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e617854f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6765cfc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647867993,0e9a52cc-ced7-421f-80c5-4d35c00d0b9c',\n",
       " 'time_used': 182,\n",
       " 'faces': [{'face_token': 'b7a70f051f3d247041fd6798513eed3a',\n",
       "   'face_rectangle': {'top': 172, 'left': 363, 'width': 167, 'height': 167}}],\n",
       " 'image_id': 'PrBvxI8BSwtGOJ1b3NVNMg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7ba3ea92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647867995,49555b47-b99a-4400-9795-10f55a3fcf08',\n",
       " 'time_used': 148,\n",
       " 'faces': [{'face_token': 'effb91c3b3633fe9b1fa473d4589ea85',\n",
       "   'face_rectangle': {'top': 250, 'left': 314, 'width': 383, 'height': 383}}],\n",
       " 'image_id': 'xYQowtV0+RL+aE0OZfaB1A==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
